package com.mao.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.mao.pojo.Target;
import com.mao.pojo.User;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Param:
 * @Thought:
 * @Author: Wenjie Mao
 * @create： 2022-11-23 19:00
 */
public class ExcelUtils {

    //向表格中写入数据
    /*
    @Param: destUrl->表格路径
    @Param: key->表头
    @Param: value->对应数据
    @Param: create_at->创建时间
     */
    @Deprecated
    public static void writeIntoExcel(String destUrl, String key, String value, String create_at){

    }
    /*
    @Param: target -> 表格所需的所有字段组成对象
    @Param: destUrl -> 需要写入的表格的URL
     */
    public static void writeIntoExcel(String destUrl, Target target){
        ArrayList<Target> list = new ArrayList<>();
        list.add(target);

        File file = new File(destUrl);
        File tempFile = new File("d://temp.xlsx");
        if(file.exists()){
           /* EasyExcel.write(file, Word.class).needHead(false).
                    withTemplate(file).file(tempFile).sheet().doWrite(list);*/
            EasyExcel.write(file,Target.class).needHead(false).
                    withTemplate(file).file(tempFile).sheet().doWrite(list);
            if (tempFile.exists()){
                file.delete();
                tempFile.renameTo(file);
            }
        }
    }

    //将全都是target的list写入表格
    public static void writeIntoExcel(String destUrl, List<Target> list){
        File file = new File(destUrl);
        File tempFile = new File("d://temp.xlsx");
        if(file.exists()){
           /* EasyExcel.write(file, Word.class).needHead(false).
                    withTemplate(file).file(tempFile).sheet().doWrite(list);*/
            EasyExcel.write(file,Target.class).needHead(false).
                    withTemplate(file).file(tempFile).sheet().doWrite(list);
            if (tempFile.exists()){
                file.delete();
                tempFile.renameTo(file);
            }
        }
    }

    public void test(){
        File file = new File("d://text.xlsx");
        File tempFile = new File("d://temp.xlsx");

        ArrayList<User> list = new ArrayList<>();
        User user1 = new User("1", "1", 1);
        User user2 = new User("2", "2", 2);
        User user3 = new User("3", "3", 3);

        list.add(user1);
        list.add(user2);
        list.add(user3);

        if(file.exists()){

           /* EasyExcel.write(file, Word.class).needHead(false).
                    withTemplate(file).file(tempFile).sheet().doWrite(list);*/
            EasyExcel.write(file,Target.class).needHead(false).
                    withTemplate(file).sheet().doFill(list);
        }else{
            EasyExcel.write(file,Target.class).needHead(true).
                    withTemplate(file).sheet().doWrite(list);
        }
    }

}
